// bookgo/borrow/choose/choose.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
      avatarUrl:'',
      aid:'',
      pid:'',
      info:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function (options) {
        console.log('-----------choose-->onLoad--->options----------');
        console.log(options);
        var that = this;
        var pid = options.pid,
            aid = options.aid;
        that.setData({
            pid:pid,
            aid:aid
        });
        if(options.pid){
            that.loadData();
        }
        console.log('--------------choose--->onLoad-->options-------------');
        console.log(options);
        var userInfo = wx.getStorageSync('userInfo'),
            avatarUrl = userInfo.avatarUrl;
            that.setData({
                avatarUrl:avatarUrl
            });
            that.loadData(pid);

    },
    loadData:function (pid) {
        var that = this;
        wx.request({
            url: 'https://2016.bookgo.com.cn/wxapp/choose/card',
            data: {
                pid:pid
            },
            success: function (res){
                console.log('------choose---->loadData---->success---->res------');
                console.log(res);
                that.setData({
                    info: res.data.data
                })
                
            }
        })
    },
    addCard: function () {
        var that = this;
        wx.redirectTo({
                url: 'addcard/addcard?pid=' + that.data.pid,
            })
    },
    // go_logout: function () {
    //     var openid = wx.getStorageSync('openid');
    //     console.log(openid);
    //     wx.showLoading({
    //         title: '请求中',
    //     });
    //     const that = this;
    //     wx.request({
    //         url: 'https://2016.bookgo.com.cn/wxapp/api/go_logout',
    //         data: {
    //             aid: that.data.aid,
    //             openid: openid
    //         },
    //         header: {
    //             'content-type': 'application/json'
    //         },
    //         success: function (res) {
    //             console.log(res.data)
    //             switch (res.data.code) {
    //                 case 101:
    //                     wx.hideLoading();
    //                     wx.clearStorageSync();
    //                     wx.reLaunch({
    //                         url: '/bookgo/index/index',
    //                     })
    //                     break;
    //                 default:
    //                     wx.hideLoading();
    //                     wx.showModal({
    //                         title: '提示',
    //                         content: res.data.data,
    //                         showCancel: false,
    //                         success: function (res) {
    //                             console.log(res);
    //                         }
    //                     })
    //             }

    //         },
    //         fail: function () {

    //         }
    //     })
    // },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */

    switchLogin: function(e) {
        var that = this;
        var switch_aid = e.currentTarget.dataset.aid,
            switch_reader_id = e.currentTarget.dataset.reader_id,
            switch_password = e.currentTarget.dataset.password;
        
        wx.request({
            url: 'https://2016.bookgo.com.cn/wxapp/api/public_login',
            data: {
                stuid: switch_reader_id,
                password: switch_password,
                aid: switch_aid
            },
            method: 'POST',
            success: function (res) {
                console.log(res);
                wx.hideLoading();
                if (res.data.code == 101) {
                    

                }
            }
        })
    },


    change_flag:function (aid,reader_id,password) {
        wx.request({
            url: 'https://2016.bookgo.com.cn/wxapp/choose/change_flag',
        })
    },




    go_logout: function () {
        const that = this;
        console.log(that.data.aid);
        var openid = wx.getStorageSync('openid');
        console.log(openid);
        wx.request({
            url: 'https://2016.bookgo.com.cn/wxapp/api/go_logout',
            data: {
                aid: that.data.aid,
                openid: openid
            },
            header: {
                'content-type': 'application/json'
            },
            success: function (res) {
                console.log(res.data)
                switch (res.data.code) {
                    case 101:
                        wx.hideLoading();
                        wx.clearStorageSync();
                        // that.go_login();
                        // that.loadData();
                        break;
                    default:
                        wx.hideLoading();
                        wx.showModal({
                            title: '提示',
                            content: res.data.data,
                            showCancel: false,
                            success: function (res) {
                                console.log(res);
                            }
                        })
                }

            },
            fail: function () {

            }
        })
    },

  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})